
function jshintApply() {
	var check = document.getElementById('jshintOption').getElementsByTagName("input");
	localStorage.jshintOption="";
	for (var i=0;i<check.length;i++) {
		if( check[i].type==='checkbox') {
			localStorage.jshintOption=localStorage.jshintOption+check[i].id+":"+check[i].checked+";";
		}
	}
}

function jshintRun(sourceCode) {
	jshintApply();
	var option = {};
	if ( localStorage.jshintOption !== 'undefined' ) {
		var str = localStorage.jshintOption.substring(0,localStorage.jshintOption.length-1).split(';');
		for(var i=0;i<str.length;i++) {
				option[str[i].split(':')[0]]=str[i].split(':')[1] == 'true'?true:false;
		}
	}
	var result = JSHINT(sourceCode,option);
	if( document.getElementById('jshintOption')){
		jshintCancel();
	}
	if( result === true ) {
		document.getElementById('error').value="no error";
		//return "no error.!";
	} else {
		var errorInfo = "";
		for (var key in JSHINT.errors) {
			if (JSHINT.errors[key] !== null )
			errorInfo = errorInfo +
						JSHINT.errors[key].evidence + 
						"  [ line " + JSHINT.errors[key].line + ":"+ 
						" char: " + JSHINT.errors[key].character + " ]" + "\n" +
						JSHINT.errors[key].reason + "\n";
		}
		document.getElementById("error").value=errorInfo;
	}
}
	//IE cancel �ò���
function jshintCancel() {
	var div = document.getElementById('jshintOption');
	document.body.removeChild(div);
}
	
function createJshintOptionDiv() {
	
    function option(id, description) {
        return '<td><input type="checkbox" id="' + id + '" />' + description + '&nbsp&nbsp&nbsp</td> ';
    }
	
	function setChecked() {
		var option = {};
		if ( localStorage.jshintOption !== 'undefined' ) {
			var str = localStorage.jshintOption.substring(0,localStorage.jshintOption.length-1).split(';');
			for(var i=0;i<str.length;i++) {
				option[str[i].split(':')[0]]=str[i].split(':')[1] == 'true'?true:false;
			}
		}
		for(key in option) {
			if(option.hasOwnProperty(key)){
				var obj=document.getElementById(key);
				if( obj != null) {
					if(option[key] === true)
						obj.setAttribute("checked", true);
					else
						obj.checked="";
				}
			}
		}
	}
	
	function initChecked() {
		document.getElementById("debug").setAttribute("checked", true);
		document.getElementById("forin").setAttribute("checked", true);
		document.getElementById("browser").setAttribute("checked", true);
		document.getElementById("eqnull").setAttribute("checked", true);
		document.getElementById("noarg").setAttribute("checked", true);
		document.getElementById("undef").setAttribute("checked", true);
		
	}
	
    var div = document.createElement("div");
    div.id = "jshintOption";
    div.innerHTML = "<h3> &nbsp&nbspJsHint options</h3><hr/>";
    var div2 = document.createElement("div");
	div2.id="jshintOptionContent";
    div2.style.height = "310";
    div2.style.overflow = "auto";
    div.appendChild(div2);
    div2.innerHTML = div2.innerHTML + '<table id="jshintOptionTable"><tr>' + option('curly', 'require curly braces around blocks') + option('bitwise', 'disallow bitwise operator') + option('eqeqeq', 'disallow == and !=') + '</tr><tr>' + option('undef', 'disallow undefined variables') + option('newcap', 'capitalize names of constructor functions') + option('noarg', 'disallow arguments.caller and arguments.callee') + '</tr><tr>' + option('noempty', 'prohibit empty blocks') + option('nonew', 'disallow constructor functions for side-effects') + option('plusplus', 'disallow ++ and --') + '</tr><tr>' + option('forin', 'disallow unfltered forin') + option('regexp', 'disallow unsafe . in regexp') + option('strict', 'require "use strict"') + '</tr><tr>' + option('asi', 'allow missing semicolon') + option('boss', 'allow assignment in if/for/...') + option('debug', 'allow debug statement') + '</tr><tr>' + option('eqnull', 'allow ==null') + option('es5', 'allow es5 features') + option('evil', 'allow eval') + '</tr><tr>' + option('funcscope', 'allow variables declared in control structure used outside that structrue') + option('globalstrict', 'allow global strict mode') + option('iterator', 'allow __iterator__') + '</tr><tr>' + option('loopfunc', 'allow function in loops') + option('multistr', 'allow multistr string') + option('onecase', 'allow switch with one case') + '</tr><tr>' + option('proto', 'allow __proto__') + option('scripturl', 'allow scripturl') + option('shadow', 'allow  variable shadowing ') + '</tr><tr>' + option('sub', 'allow safe subset') + option('browser', 'defines globals exposed by browser') + option('couch', 'defines globals exposed by couchDB') + '</tr><tr>' + option('devel', 'defines globals that are used for debugging') + option('dojo', 'defines globals exposed by the Dojo Toolkit') + option('jquery', 'defines globals exposed by jquery') + '</tr><tr>' + option('mootools', 'defines globals exposed by the MooTools') + option('node', 'defines globals available in nodejs') + option('nonstandard', 'defines non-standard but widely adopted globals') + '</tr><tr>' + option('prototypejs', 'defines globals exposed by the Prototype ') + option('rhino', ' defines globals available in rhino') + option('wsh', 'defines globals available in windows script hosts') + '</tr><tr>' + option('nomen', 'disallow _ in identifier') + option('onevar', 'allows only one var statement per function') + option('passfail', 'stop on the first error') + '</tr><tr>' + option('white', 'strict white space') + '</tr></table>';
    for (var j = 50; j > 0; j--)
		div2.innerHTML = div2.innerHTML + "&nbsp";
    var moreButton = document.createElement('input');
    moreButton.type = 'button';
    moreButton.value = "      run      ";
    moreButton.name = "run";
    moreButton.setAttribute("onclick", "control.runJshint()");
    div2.appendChild(moreButton);
    var okButton = document.createElement('input');
    okButton.type = 'button';
    okButton.value = "      apply       ";
    okButton.name = "applyButton";
    okButton.setAttribute("onclick", "jshintApply()");
    div2.appendChild(okButton);
    var cancelButton = document.createElement('input');
    cancelButton.type = 'button';
    cancelButton.value = "     cancel     ";
    cancelButton.name = "cancelbutton";
    cancelButton.setAttribute("onclick", "jshintCancel()");
    div2.appendChild(cancelButton);
    // try {
        // div.style.background = "red";
        // div.style.position = "absolute";
        // div.style.width = "800";
        // div.style.height = "400";
        // var left = document.body.clientWidth / 2 - 400;
        // var top = document.body.clientHeight / 2 - 150;
		// div.style.cssText = div.style.cssText + "left:"+left+";top:"+top+";";
        // div.style.left = left;
        // div.style.top = top;
        	// div.style.overflow="auto";
        // div.style.cssText = "border-radius:1em;" + div.style.cssText;
        // div.style.background = "rgba(220,220,220,0.8)";
        // div.sytle.zIndex = "99999";
    // } catch (e) {
        // div.style.background = "rgb(240,240,240)";
    // }
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    document.body.appendChild(div);
	if (localStorage.jshintOption !== 'undefined') {
		setChecked();
	} else {
		initChecked();
	}
}